/**
  * Created by Joshua Conero(O'Uymas) on 2016/12/13 0013.
  * QQ: 957376074
  * E-mail: Brximl@163.com
  */
package org.yang
import java.awt.Dimension
import scala.swing.Swing._
import scala.swing._
import scala.swing.event.{ButtonClicked, EditDone}
class HomeNewFile(helper:Common) {
  val br = "\r\n"
  val quote = "\""
  // 文件编辑框
  val fileCreater = new TextArea(){
    columns = 400
    rows = 100
    editable = false
    preferredSize = new Dimension(550,400)
    border = Swing.EmptyBorder(5,5,5,5)
  }
  val fcPane = new ScrollPane(){
    contents = fileCreater
    preferredSize = new Dimension(400,300)
    border = Swing.EmptyBorder(5,5,5,5)
  }
  /*
  val headers = Array("代码","名称","文本")
  //val first = Array(List("Mary", "Campione", "Snowboarding", 5, false).toArray)
  val first = Array(List("Mary", "Campione", "Snowboarding",1,false).toArray)
  val formTable = new Table(first,headers){
  }
  */
  val model = Array(List("Mary", "Campione", "Snowboarding", 5, false).toArray,
    List("Alison", "Huml", "Rowing", 5, false).toArray,
    List("Kathy", "Walrath", "Knitting", 5, false).toArray,
    List("Sharon", "Zakhour", "Speed reading", 5, false).toArray,
    List("Philip", "Milne", "Pool", 5, false).toArray)

  val formTable = new Table(model, Array("First Name", "Last Name", "Sport", "# of Years", "Vegetarian")) {
    preferredViewportSize = new Dimension(500, 70)
  }

  new Frame() {
    title = "crawler文件新增"
    contents = new BoxPanel(Orientation.Horizontal) {
      border = CompoundBorder(TitledBorder(EtchedBorder, "新建爬虫规则"), EmptyBorder(5, 100, 20, 300))
      val save_btn = new Button("保存")
      val run_btn = new Button("执行")
      val close_btn = new Button("关闭")
      val url_ipt = new TextField(20)
      val div = new TextField(20)
      val listValue = new TextField(20)
      // form
      contents += new BoxPanel(Orientation.Vertical) {
        contents += formTable
        contents += new Label("url")
        contents += url_ipt
        contents += new Label("区域")
        contents += div
        contents += new Label("项值")
        contents += listValue
        contents += run_btn
        contents += save_btn
        contents += close_btn
        contents += close_btn
        listenTo(save_btn, run_btn, close_btn,url_ipt,listValue)
        reactions += {
          case EditDone(`url_ipt`) =>
            val value = url_ipt.text
            if(value != "") {
              fileCreater.text = "<url>" + value + "</url>"
            }
          case EditDone(`listValue`) =>
            val tag = listValue.text
            var scope = div.text
            if(tag != "") {
              scope = if (scope == "") "<result>" else "<result scopen=" +quote+scope + quote+ ">"
              fileCreater.append(br+scope + "<" + tag + "></" + tag + ">"+br+"</result>")
            }
          case ButtonClicked(`save_btn`) =>
            val text = fileCreater.text
          //helper.put_content("/runtime/test.xml",text)
          case ButtonClicked(`run_btn`) =>
            val text = fileCreater.text
            println(text)
          case ButtonClicked(`close_btn`) =>
            dispose()
            println(8 * 0.14)
        }
        visible = true
      }
      // 编辑域
      contents += fcPane
      visible = true
    }
    preferredSize = new Dimension(800, 500)
    visible = true
  }
}
